import java.util.BitSet;
import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: wangxin
 * Date: 2024-12-22
 * Time: 18:13
 */
class SimpleHash{
    public int cap;
    public int seed;
    public SimpleHash(int cap,int seed){
        this.cap = cap;
        this.seed = seed;
    }
    final int hash(String key) {
        int h;
        return (key == null) ? 0 :(seed*(cap - 1)) & ((h = key.hashCode()) ^ (h >>> 16));
    }
}

public class MyBloomFilter {
    public static final int DEFAULT_SIZE = 1 << 20;
    public BitSet bitSet;
    public int userSize;
    public static final int[] seeds = {5,7,11,13,27,33};
    public SimpleHash[] simpleHashes;
    public MyBloomFilter(){
        bitSet = new BitSet(DEFAULT_SIZE);
        simpleHashes = new SimpleHash[seeds.length];
        for (int i = 0 ;i < simpleHashes.length;i++){
            simpleHashes[i] = new SimpleHash(DEFAULT_SIZE,seeds[i]);
        }
    }
    public void add(String val){
        for (SimpleHash simpleHash:simpleHashes) {
           int index = simpleHash.hash(val);
            bitSet.set(index);
        }
    }
    public boolean contains(String val){
        for (SimpleHash simpleHash:simpleHashes) {
            int index = simpleHash.hash(val);
           if(!bitSet.get(index)){
               return false;
           }

        }
        return true;
    }
    public static void main(String[] args) {
        MyBloomFilter myBloomFilter = new MyBloomFilter();
        myBloomFilter.add("hello");
        myBloomFilter.add("hell");
        myBloomFilter.add("hllo");
        myBloomFilter.add("helo");
        System.out.println(myBloomFilter.contains("hell"));
        System.out.println(myBloomFilter.contains("helc"));
        System.out.println(myBloomFilter.contains("hal"));
    }
}
